package com.iotechn.unimall.app.api.drivercar;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AppServiceException;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.AreaDO;
import com.iotechn.unimall.data.domain.DrvierCarDO;
import com.iotechn.unimall.data.enums.SystemStatusType;
import com.iotechn.unimall.data.enums.drivercar.DriverCarApplyType;
import com.iotechn.unimall.data.mapper.AreaMapper;
import com.iotechn.unimall.data.mapper.DrvierCarMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DriverCarApplyappserviceImpl implements DriverCarApplyappservice {

    @Autowired
    private DrvierCarMapper drvierCarMapper;
    @Autowired
    private AreaMapper areaMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object createDriverCarApply(Long userId, DrvierCarDO drvier) throws ServiceException {
        Date now = new Date();
        DrvierCarDO drvierCarDO = new DrvierCarDO();
        drvierCarDO.setGmtCreate(now);
        drvierCarDO.setGmtUpdate(now);
        drvierCarDO.setStatus(SystemStatusType.ENABLE.getCode());
        drvierCarDO.setApplyStatus(DriverCarApplyType.CREATE.getCode());
        drvierCarDO.setCarNumber(drvier.getCarNumber());
        drvierCarDO.setCarDetail(drvier.getCarDetail());
        drvierCarDO.setCarImg(drvier.getCarImg());
        drvierCarDO.setTravelCardImg(drvier.getTravelCardImg());
        drvierCarDO.setDriverCardImg(drvier.getDriverCardImg());
        drvierCarDO.setTrueName(drvier.getTrueName());
        drvierCarDO.setContactPhone(drvier.getContactPhone());
        drvierCarDO.setUserId(userId);
        if (drvierCarDO.getAreaId() != null) {
            AreaDO areaDO = areaMapper.selectById(drvier.getAreaId());
            if (areaDO != null) {
                drvierCarDO.setArea(areaDO.getAreaName());
            }
        }
        return drvierCarMapper.insert(drvierCarDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object updateDriverCarApply(Long userId, DrvierCarDO drvier) throws ServiceException {
        if (null != drvier.getApplyStatus() && DriverCarApplyType.PASS.getCode() == drvier.getApplyStatus().intValue()) {
            throw new AppServiceException("当前状态不可修改", 500);
        }
        drvier.setStatus(SystemStatusType.ENABLE.getCode());
        Date now = new Date();
        drvier.setGmtUpdate(now);
        drvier.setApplyStatus(DriverCarApplyType.CREATE.getCode());

        return drvierCarMapper.updateById(drvier);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object deleteDriverCarApply(Long userId, Long drvierId) throws ServiceException {
        DrvierCarDO drvierCarDO = drvierCarMapper.selectById(drvierId);
        if (DriverCarApplyType.PASS.getCode() == drvierCarDO.getApplyStatus().intValue()) {
            throw new AppServiceException("当前状态不可删除", 500);
        }
        return drvierCarMapper.deleteById(drvierId);
    }

    @Override
    public Page<DrvierCarDO> driverCarPage(Integer page, Integer limit) throws ServiceException {

        Wrapper<DrvierCarDO> wrapper = new EntityWrapper<DrvierCarDO>();
        /**if (!StringUtils.isEmpty(name)) {
         wrapper.like("username", name);
         }**/
        wrapper.orderBy("id", false);
        Integer count = drvierCarMapper.selectCount(wrapper);
        List<DrvierCarDO> PlantCarLogDOS = drvierCarMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(PlantCarLogDOS, page, limit, count);
    }

    @Override
    public Object reserveDriver(Long userId) throws ServiceException {
        return null;
    }
}
